<?php require_once('../Connections/conex.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validacion de usuario
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['user'])) {
  $loginUsername=$_POST['user'];
  $password=$_POST['pass'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "empresa_index.php";
  $MM_redirectLoginFailed = "empresa_login.php?errorlogin=true";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_conex, $conex);
  
  $LoginRS__query=sprintf("SELECT usuario, contrasena FROM empresas WHERE usuario=%s AND contrasena=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $conex) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	 
	     

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
} 


if($_GET['errorlogin'] == 'true'){ //si se ingresa mal la contraseña o el usuario
	$errordice = "<p>Nombre de usuario o contraseña incorrectos</p>";
}


?>

<!-- empieza la página-->
<?php
$PAGE = 'empresa_login';
$TIPO = 'backend';
$TITLE = 'Ingresar como empresa';
?>
   
   <?php include('../includes/head.php') ?>
   
    <body id="<?php echo $PAGE; ?>" class="<?php echo $PAGE; ?>">
     
     <?php include('../includes/cabecera.php') ?>
       
     <?php include('../includes/intro.php') ?>
        
     <?php if ($PAGE == 'home') { ?>
     <?php include('../includes/cols.php') ?>
     <?php } ?>
        
     <div id="contenido">
     
     	<div id="main">
        
            <h2>Ingresar como empresa</h2>
            
            <?php if($_GET['register'] == 'true') {?>
                <p>Su Empresa ha sido cargada con éxito! Ingrese aquí sus datos de acceso</p>
            <?php } ?>
            
            
            <form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
              <p>
                <label for="user">Usuario</label>
                <input type="text" name="user" id="user">
              </p>
              <p>
                <label for="pass">Contraseña</label>
                <input type="password" name="pass" id="pass">
              </p>
              <p>
                <input type="submit" name="submit" id="submit" value="Ingresar">
              </p>
            </form>
            
            <?php echo $errordice; ?>

		</div><!-- fin main-->
        
        <div id="columna">
   			<?php include('../includes/columna.php') ?>
        </div>
         
            
     </div>
        
     <hr class="noscreen" />

        
     <?php include('../includes/pie.php') ?>

<!-- termina la página-->